package com.openatc.agent.controller;

import com.openatc.agent.model.Permission;
import com.openatc.agent.service.PermissionDao;
import com.openatc.core.model.RESTRetBase;
import com.openatc.core.util.RESTRetUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth/permission")
//@RequiresPermissions("sys:auth:permission:view")
public class PermissionController {

    @Autowired
    private PermissionDao permissionService;


    @GetMapping("")
    public RESTRetBase listPermission() {
        return RESTRetUtils.successObj(permissionService.findAll());
    }

    @PostMapping("")
//    @RequiresPermissions("sys:auth:permission:modify")
    public RESTRetBase addPermission(@RequestBody Permission sysPermission) {
        return RESTRetUtils.successObj( permissionService.save(sysPermission) );
    }

    @DeleteMapping("/{permissionId}")
//    @RequiresPermissions("sys:auth:permission:modify")
    public RESTRetBase delPermission(@PathVariable int permissionId) {
        try{
            permissionService.deleteById(permissionId);
        }
        catch (Exception e){
            return RESTRetUtils.errorObj("9999",e.getMessage(),"/auth/permission" + permissionId,null);
        }
        return RESTRetUtils.successObj();
    }

    @GetMapping("/{permissionId}")
    public RESTRetBase getPermission(@PathVariable int permissionId) {
        return RESTRetUtils.successObj( permissionService.findById(permissionId) );
    }

    @PutMapping("")
//    @RequiresPermissions("sys:auth:permission:modify")
    public RESTRetBase putPermission(@RequestBody Permission sysPermission) {
        return RESTRetUtils.successObj( permissionService.save(sysPermission) );
    }

//    @GetMapping("/{permissionPath}/users")
//    public RESTRetBase getUsersByPermission(@PathVariable String permissionPath) {
//        return permissionService.getUsersByPermission(permissionPath);
//    }
//
//    @GetMapping("/{permissionPath}/roles")
//    public RESTRetBase getRolesByPermission(@PathVariable String permissionPath) {
//        return permissionService.getRolesByPermission(permissionPath);
//    }


}
